﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using LyricTrackerPlugin.Properties;
using Slippor.Common.PluginFramework;
using Slippor.Douban.Radio.Assist;
using Slippor.Douban.Radio.Tracker;
using System.Drawing;

namespace LyricTrackerPlugin
{
    public class LyricTracker : ITracker, IHotkeyExcutor
    {
        LyricFrom _lyricForm;

        public void ShowLyric()
        {
            if (_lyricForm == null || _lyricForm.IsDisposed)
            {
                _lyricForm = new LyricFrom(this);
                _lyricForm.SetSong(_currentSong);
                _lyricForm.SetLyric(_currentLyric);
                IsShow = true;
            }
            _lyricForm.Show();
            if (_lyricForm.WindowState == FormWindowState.Minimized)
            {
                _lyricForm.WindowState = FormWindowState.Normal;
            }
            _lyricForm.Activate();
            _lyricForm.BringToFront();
        }

        #region ITracker Members
        private const String _key = "Lyric";
        public String Name
        {
            get { return _key; }
        }

        private readonly Stopwatch _stopwatch = new Stopwatch();
        public TimeSpan Elapsed
        {
            get { return _stopwatch.Elapsed.Add(_justTimeSpan); }
        }

        private Song _currentSong;
        private LyricCollection _currentLyric;
        private TimeSpan _justTimeSpan;
        public void Track(Song song)
        {
            if (_currentSong != song)
            {
                _currentSong = song;
                if (_stopwatch.IsRunning)
                {
                    _stopwatch.Stop();
                }
                _stopwatch.Reset();
                _justTimeSpan = DateTime.Now.Subtract(song.ListenTime);
                _stopwatch.Start();
                if (_lyricForm != null && !_lyricForm.IsDisposed)
                {
                    _lyricForm.Clear();
                }
                UpdateLyric(song);
            }
        }

        public void TrackUpdate(ICommand command, Song song)
        {
            if (song != null)
            {
                switch (command.Command)
                {
                    case Command.Other:
                        UpdateLyric(song);
                        break;
                    default:
                        break;
                }
            }
        }

        private readonly ILrcer _lrcer = new Lrcer();
        private void UpdateLyric(Song song)
        {
            if (_lyricForm != null && !_lyricForm.IsDisposed)
            {
                _lyricForm.SetSong(_currentSong);
            }
            if (song.Album != null && !String.IsNullOrEmpty(song.Album.ArtistString))
            {
                using (var downloadLrcWorker = new BackgroundWorker())
                {
                    downloadLrcWorker.DoWork += (sender, e) =>
                        {
                            e.Result = _lrcer.DownloadLrc(song);
                        };
                    downloadLrcWorker.RunWorkerCompleted += (sender, e) =>
                        {
                            if (e.Result != null)
                            {
                                if (_currentLyric != null)
                                {
                                    _currentLyric.Dispose();
                                }
                                _currentLyric = new LyricCollection(e.Result.ToString());
                            }
                            else
                            {
                                _currentLyric = null;
                            }
                            if (_lyricForm != null && !_lyricForm.IsDisposed)
                            {
                                _lyricForm.SetLyric(_currentLyric);
                            }
                        };
                    downloadLrcWorker.RunWorkerAsync();
                }
            }
        }

        public void Clear()
        {
        }

        public void Pause()
        {
            if (_stopwatch.IsRunning)
            {
                _stopwatch.Stop();
            }
        }

        public void Resume()
        {
            if (!_stopwatch.IsRunning)
            {
                _stopwatch.Start();
            }
        }

        public void Stop()
        {
            if (_stopwatch.IsRunning)
            {
                _stopwatch.Stop();
            }
            _currentSong = null;
        }
        #endregion

        public void Research()
        {
            if (_currentSong != null)
            {
                UpdateLyric(_currentSong);
            }
        }

        #region Properties
        private readonly Settings _settings = LyricTrackerConfig.SettingManager.Settings as Settings;

        public int ShowMode
        {
            get { return _settings.ShowMode; }
            set { _settings.ShowMode = value; _settings.Save(); }
        }

        public bool TopMost
        {
            get { return _settings.TopMost; }
            set { _settings.TopMost = value; _settings.Save(); }
        }

        public bool IsShow
        {
            get { return _settings.IsShow; }
            set { _settings.IsShow = value; _settings.Save(); }
        }

        public Point Position
        {
            get { return _settings.Position; }
            set { _settings.Position = value; _settings.Save(); }
        }
        #endregion

        #region IHotkeyExcutor Members
        public String Hotkey
        {
            get
            {
                return _settings.ShortCutKey;
            }
            set
            {
                _settings.ShortCutKey = value;
                _settings.Save();
            }
        }

        public void Excute()
        {
            ShowLyric();
        }
        #endregion

        public Font Font { get { return _settings.Font; } }

        public LyricTracker()
        {
            _settings.Upgrade();
            _settings.SettingsSaving += (sender, e) =>
                {
                    if (_lyricForm != null && !_lyricForm.IsDisposed)
                    {
                        _lyricForm.RefreshStyle();
                    }
                };
        }

        public Color FontColor { get { return _settings.Color; } }
    }
}
